package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.User;
import cn.wolfcode.shop.api.service.IUserService;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "用户注册接口", description = "提供用户及其资源相关的操作方法")
@RestController
@RequestMapping("api/users")
public class UserController {
    @Reference
    private IUserService userService;

    @ApiOperation(value = "用户注册接口", notes = "提供用户注册服务,更具传入的用户实例创建新的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "form", dataType = "string", required = true),
    })
    @RequestMapping(method = RequestMethod.POST)
    public JsonResultVo Register(@ApiIgnore User user) {
        JsonResultVo resultVo = new JsonResultVo();
        try {
            resultVo.setResult(userService.register(user));
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }

    @ApiOperation(value = "用户登陆接口", notes = "提供用户登陆服务,根据传入的用户名及密码验证用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "tokens", method = RequestMethod.POST)
    public JsonResultVo login(String userName, String password) {
        JsonResultVo resultVo = new JsonResultVo();
        try {
            resultVo.setResult(userService.login(userName, password));
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }

    @ApiOperation(value = "用户登出接口", notes = "提供用户注销服务,根据传入的token证用户信息,并注销该用户登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token_in_head", value = "登陆密钥", paramType = "header", dataType = "string", required = true)
    })
    @RequestMapping(value = "tokens", method = RequestMethod.DELETE)
    public JsonResultVo logout(@RequestHeader("token_in_head") String token) {
        JsonResultVo resultVo = new JsonResultVo();
        try {
            resultVo.setResult(userService.logout(token));
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }
}
